<?php
require_once(__PHY_LIB.'/class/Item.class.php');

class uWorkSize extends Item {
/// definition and constructor ///
    function uWorkSize($id=0, $field="") {
        Query::Query();
        $this->cfgKeyField("id");
        $this->cfgAllField("id,work_id,rank,title,title_en,kuang,price,price_en,description,description_en");
        $this->cfgTable("unit_work_size");
        if ("" == $field || "*" == $field) {
            $this->cfgField($this->mAllField);
        }
        else {
            $this->cfgField($field);
        }
        if (0 < $id) {
            $this->cfgWhere($this->mKeyField . "=" . $id);
            $this->load();
        }
        else {
            $this->set($this->mKeyField, $id);
        }
    }

    function invalid() {
        if (!$this->get('work_id')) {
            return 'work_id';
        }

        return false;
    }

	function delete() {
		if (!(0 < $this->getID())) { return false; }
		/// 删除子表 ///
		$sQ = "DELETE FROM unit_work_size_kuang WHERE size_id='".$this->getID()."'";
		$this->loadQuery($sQ);
		$sQ = "DELETE FROM unit_work_sku WHERE size_id='".$this->getID()."'";
		$this->loadQuery($sQ);
		/// 排序调整 ///
		$oList = new uWorkSizeList();
		$oList->cfgWhere("work_id='".$this->get('work_id')."' AND rank>'".$this->get('rank')."'");
		$oList->load();
		while ($o = $oList->nextItem()) {
			$o->set('rank', $o->get('rank') - 1); // 大于的统统-1
			$o->update();
		}
		/// 删除自己 ///
		return Query::delete();
	}

/// 排序相关 ///
	function calcNewRank() {
		$sQ = "SELECT MAX(rank) max_rank FROM unit_work_size WHERE work_id='".$this->get('work_id')."'";
		$this->loadQuery($sQ);
		$r = $this->nextRecord(false);
		return (int)$r['max_rank'] + 1;
	}
	function rankTo($nRank) {
		if (!(0 < $this->getID())) { return false; }
		$nRank = max(1, (int)$nRank);
		if ($nRank > $this->get('rank')) { // 增大
			$oList = new uWorkSizeList();
			$oList->cfgWhere("work_id='".$this->get('work_id')."' AND rank>'".$this->get('rank')."' AND rank<='{$nRank}'");
			$oList->load();
			while ($o = $oList->nextItem()) {
				$o->set('rank', $o->get('rank') - 1); // 中间的统统-1
				$o->update();
			}
		}
		elseif ($nRank < $this->get('rank')) { // 减小
			$oList = new uWorkSizeList();
			$oList->cfgWhere("work_id='".$this->get('work_id')."' AND rank<'".$this->get('rank')."' AND rank>='{$nRank}'");
			$oList->load();
			while ($o = $oList->nextItem()) {
				$o->set('rank', $o->get('rank') + 1); // 中间的统统+1
				$o->update();
			}
		}
		else { // 相等，没变动
			return true;
		}
		/// 修改自己 ///
		$this->set('rank', $nRank);
		$this->update();
		return true;
	}

} /// end class uWorkSize.


class uWorkSizeList extends ItemList {
    /// definition and constructor ///
    function uWorkSizeList($tablename=null) {
        ItemList::ItemList();
        $this->cfgClass("uWorkSize");
        $this->cfgKeyField("id");
        $this->cfgAllField("id,work_id,rank,title,title_en,kuang,price,price_en,description,description_en");
        $this->cfgTable(null == $tablename ? "unit_work_size" : $tablename);
    }

} /// end class uWorkSizeList.


/// related functions ///
function uWorkSize_i18n($nID, $sField) {
	static $rCache = array();
	if (!isset($rCache['LangFields'])) {
		$rCache['LangFields'] = '|title|description|price';
	}
	if (is_array($nID)) {
		$rCache['This'] = $nID;
	}
	elseif (!isset($rCache[$nID])) {
		$oOne = new uWorkSize($nID);
		$rCache[$nID] = $oOne->getData();
		$rCache['This'] =& $rCache[$nID];
	}
	else {
		$rCache['This'] =& $rCache[$nID];
	}
	if ('en' == $_SESSION['LANG'] && strpos($rCache['LangFields'], $sField)) {
		$sField .= '_en';
		return $rCache['This'][$sField];
	}
	else { return $rCache['This'][$sField]; }
}
?>
